﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Web;
using A.Enum;
using Fashion.Models;

namespace Fashion.Common.Method
{
    public class EnhancedPrincipal : IPrincipal
    {
        private readonly IIdentity _identity;
        private readonly Account _account;
        public static readonly EnhancedPrincipal Anonymous = new AnonymousPrincipal();

        public EnhancedPrincipal(IIdentity identity, Account account)
        {
            _identity = identity;
            _account = account;
        }

        public Account Data
        {
            get { return _account; }
        }

        public bool IsInRole(string role)
        {
            if (UserRole.SuperAdmin.ToString().Equals(role))
                return _account.UserRole == (int)UserRole.SuperAdmin;
            if (UserRole.Admin.ToString().Equals(role))
                return _account.UserRole == (int)UserRole.SuperAdmin || _account.UserRole == (int)UserRole.Admin;
            return false;
        }

        public IIdentity Identity
        {
            get { return _identity; }
        }

        private class AnonymousIdentity : IIdentity
        {
            public string Name
            {
                get { return "Anonymous"; }
            }

            public string AuthenticationType
            {
                get { return null; }
            }

            public bool IsAuthenticated
            {
                get { return false; }
            }
        }

        private class AnonymousPrincipal : EnhancedPrincipal
        {
            public AnonymousPrincipal() : base(new AnonymousIdentity(), new Account{Id = 0,UserName = "Guest"})
            {
            }
        }
    }
}